//
//  JTUser.m
//  囧途
//
//  Created by 杨璟 on 16/3/11.
//  Copyright © 2016年 fly. All rights reserved.
//

#define USER_LOC [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"currentUser.archive"]

#import <AVOSCloud.h>
#import <MJExtension.h>

#import "JTConst.h"

#import "JTUser.h"

@interface JTUser ()

@end

@implementation JTUser

MJExtensionCodingImplementation

- (NSString *)description {
    
    NSString *str = [NSString stringWithFormat:@"JTUser:[\n objectId=%@,\n username=%@,\n nickname=%@,\n gender=%@,\n iconpath=%@\n]", self.objectId, self.username, self.nickname, self.gender, self.iconPath];
    
    return str;
}

+ (void)logOut {
    [AVUser logOut];
    
    NSFileManager *mgr = [NSFileManager defaultManager];
    [mgr removeItemAtPath:USER_LOC error:nil];
}

+ (instancetype)currentUser {
    return [NSKeyedUnarchiver unarchiveObjectWithFile:USER_LOC];
}

+ (instancetype)userWithObjectId:(NSString *)objectId {
    JTUser *oneUser = [[JTUser alloc] init];
    if (objectId) {
        AVQuery *query = [AVQuery queryWithClassName:kJTClassUser];
        AVUser *user = [query getObjectWithId:objectId];
        
        oneUser = [JTUser userWithCurrentUser:user];
    }
    
    return oneUser;
}

+ (instancetype)userWithAVUser:(AVUser *)user {
    return [self userWithObjectId:user.objectId];
}

+ (instancetype)userWithCurrentUser:(AVUser *)user {
    JTUser *oneUser = [[JTUser alloc] init];
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        oneUser.username = user.username;
        oneUser.objectId = user.objectId;
        oneUser.createAt = user.createdAt;
        oneUser.updateAt = user.updatedAt;
        oneUser.mobilePhoneNumber = user.mobilePhoneNumber;
        oneUser.mobilePhoneVerified = user.mobilePhoneVerified;
        oneUser.nickname = user[@"NickName"];
        oneUser.gender = user[@"Gender"];
        oneUser.audit =user[@"audit"];
        oneUser.carAudit = user[@"DriverAudit"];
        oneUser.sumMoney =user[@"Sum"];
        oneUser.current =user[@"current"];
        
        AVFile *file = user[@"HeadImage"];
        NSString *tmp = [file getThumbnailURLWithScaleToFit:YES width:200 height:200];
        oneUser.iconPath = [NSURL URLWithString:tmp];
        
        if (oneUser.carAudit) {
            oneUser.isDriver = YES;
        } else {
            oneUser.isDriver = NO;
        }
    });
    dispatch_group_async(group, queue, ^{
        AVQuery *queryDriver = [AVQuery queryWithClassName:@"Driver"];
        [queryDriver whereKey:@"DriverId" equalTo:user.objectId];
        [queryDriver getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            AVObject * car = object[@"CarInfo"];
            if (car) {
                AVQuery *query = [AVQuery queryWithClassName:@"Car"];
                [query whereKey:@"objectId" equalTo:car.objectId];
                [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
                    oneUser.carName = object[@"CarName"];
                    oneUser.carNumber = object[@"CarNumber"];
                }];
            }
        }];
        
    });
    dispatch_group_notify(group, queue, ^{
        NSLog(@"记录一个User：%@", oneUser.objectId);
    });
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    return oneUser;
}

- (void)save {
    [NSKeyedArchiver archiveRootObject:self toFile:USER_LOC];
}

#pragma mark - 懒加载

- (NSData *)icon {
    if (!_icon) {

        _icon = [NSData dataWithContentsOfURL:_iconPath];
    }
    return _icon;
}

//- (NSString *)carName {
//    if (!_carName) {
//        AVQuery *queryDriver = [AVQuery queryWithClassName:@"Driver"];
//        [queryDriver whereKey:@"DriverId" equalTo:self.objectId];
//        AVObject *driver = [queryDriver getFirstObject];
//        AVObject * car = driver[@"CarInfo"];
//        if (car) {
//            AVQuery *query = [AVQuery queryWithClassName:@"Car"];
//            [query whereKey:@"objectId" equalTo:car.objectId];
//            AVObject * carInfo = [query getFirstObject];
//            _carName = carInfo[@"CarName"];
//        }
//
//    }
//    return _carName;
//}
//
//- (NSString *)carNumber {
//    if (!_carNumber) {
//        AVQuery *queryDriver = [AVQuery queryWithClassName:@"Driver"];
//        [queryDriver whereKey:@"DriverId" equalTo:self.objectId];
//        AVObject *driver = [queryDriver getFirstObject];
//        AVObject * car = driver[@"CarInfo"];
//        if (car) {
//            AVQuery *query = [AVQuery queryWithClassName:@"Car"];
//            [query whereKey:@"objectId" equalTo:car.objectId];
//            
//            AVObject * carInfo = [query getFirstObject];
//            _carNumber = carInfo[@"CarNumber"];
//        }
//    }
//    return _carNumber;
//}

@end
